﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using RacePlatformer;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Dwarfs.Screens;

namespace Dwarfs.Manager
{
   public class SoundManager
    {
        public Song backgroundSound;
        public Song introSound, menuSound, pauseScreenSound;
        private MainGame maingame;
        public Dictionary<Screen, Song> screenSongDictionary;
        public SoundManager(MainGame maingame)
        {
            screenSongDictionary = new Dictionary<Screen, Song>();
            this.maingame = maingame;
        }
        public void playSound()
        {
            MediaPlayer.Play(backgroundSound);
        }
        public void playBackgroundSound(Screen screen = null)
        {
            //check if we're in a menu
            if (screen == null)
            {
                if (MediaPlayer.State != MediaState.Playing)
                {
                    MediaPlayer.Play(backgroundSound);
                    MediaPlayer.IsRepeating = true;
                    MediaPlayer.Volume = 0.4f;
                }
            }
            else
            {
                if (MediaPlayer.State != MediaState.Playing)
                {
                    MediaPlayer.Play((Song)screenSongDictionary[screen]);
                    MediaPlayer.IsRepeating = true;
                    MediaPlayer.Volume = 0.4f;
                }
            }
        }
        public static void stopBackgroundSound()
        {
            MediaPlayer.Pause();
        }
        public static void playSound(SoundEffectInstance effect, GameObject soundObject, Vector2 position, bool hasDistance = false)
        {
            if (hasDistance)
            {
                effect.Play();
            }
            else
            {
                effect.Play();
            }
        }
        public void loadScreenSounds()
        {
            introSound = maingame.Content.Load<Song>("music/menu/intro");
            menuSound = maingame.Content.Load<Song>("music/menu/menu");
            pauseScreenSound = maingame.Content.Load<Song>("music/menu/pause");

            screenSongDictionary.Add(maingame.screenManager.introScreen, introSound);
            screenSongDictionary.Add(maingame.screenManager.menuScreen, menuSound);
            screenSongDictionary.Add(maingame.screenManager.pauseScreen, pauseScreenSound);
        }
        public void loadContent(int levelID)
        {
            backgroundSound = maingame.Content.Load<Song>("music/level"+levelID+"/background");
        }
    }
}
